// 注册页的逻辑
$('form').on('submit', async e => {
  e.preventDefault()
  // 获取表单参数进行校验
  // 用户名, 密码, 确认密码, 昵称
  const username = $('.username').val()
  const password = $('.password').val()
  const rPassword = $('.rPassword').val()
  const nickname = $('.nickname').val()
  // 表单验证
  if (!username || !password || !rPassword || !nickname) {
    return $('.error').show().text('表单字段必填!')
  }
  if (password !== rPassword) {
    return $('.error').show().text('两次输入密码不一致!')
  }
  $('.error').hide().text('')
  const res = await $.ajax({
    url: '/users/register',
    method: 'POST',
    data: $('form').serialize()
  })
  
  if (res.code === 0) return $('.error').show().text(res.message)
  if (res.code === 1) {
    alert('注册成功, 即将跳转登录')
    return window.location.href = '/pages/login'
  }
})